package com.itheima.web.manager;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateUtil;
import com.itheima.domain.db.UserInfo;
import com.itheima.domain.mongo.Comment;
import com.itheima.domain.mongo.Movement;
import com.itheima.service.db.UserInfoService;
import com.itheima.service.mongo.CommentService;
import com.itheima.service.mongo.MovementService;
import com.itheima.vo.CommentVo;
import com.itheima.vo.MovementVo;
import com.itheima.vo.PageBeanVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.bson.types.ObjectId;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Component
public class UserManager {

    @DubboReference
    UserInfoService userInfoService;

    @DubboReference
    MovementService movementService;

    @DubboReference
    CommentService commentService;

    public ResponseEntity findByPage(Integer pageNum, Integer pageSize) {
        PageBeanVo pageBeanVo = userInfoService.findByPage(pageNum, pageSize);
        return ResponseEntity.ok(pageBeanVo);
    }


    public ResponseEntity findById(Long userId) {
        UserInfo userInfo = userInfoService.findByUserId(userId);
        return ResponseEntity.ok(userInfo);
    }

    public ResponseEntity findMovementByPage(Integer pageNum, Integer pageSize, Long userId, Integer state) {
        //1.远程调用service查询当前页数据
        PageBeanVo pageBeanVo = movementService.findMovementByPage(userId, state, pageNum, pageSize);

        //2.若userId不为空,查询userInfo
        UserInfo userInfo = null;
        if (userId != null) {
            userInfo = userInfoService.findByUserId(userId);
        }

        //3.获取分页中items,遍历,获取动态
        List<MovementVo> voList = new ArrayList<>();

        List<Movement> items = (List<Movement>) pageBeanVo.getItems();
        if (CollUtil.isNotEmpty(items)) {
            for (Movement movement : items) {
                //4.若userId为空,再根据动态中的发布人查询userInfo
                if (userId == null) {
                    userInfo = userInfoService.findByUserId(movement.getUserId());
                }

                //5.将userInfo和动态封装成vo对象,将vo放入voList中
                MovementVo vo = new MovementVo();
                //先userInfo 后movement
                vo.setUserInfo(userInfo);
                vo.setMovement(movement);

                //重新格式化发布时间
                vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

                voList.add(vo);
            }
        }

        //6.将voList设置给pageBeanVo,且返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }

    public ResponseEntity findMovementById(String publishId) {
        //1.根据动态id查询movement
        Movement movement = movementService.findById(new ObjectId(publishId));

        //2.根据动态发布人id查询userInfo
        UserInfo userInfo = userInfoService.findByUserId(movement.getUserId());

        //3.封装MovementVo对象且返回
        MovementVo vo = new MovementVo();
        //先userInfo 后movement
        vo.setUserInfo(userInfo);
        vo.setMovement(movement);
        //重新格式化发布时间
        vo.setCreateDate(DateUtil.formatDateTime(new Date(movement.getCreated())));

        return ResponseEntity.ok(vo);
    }

    public ResponseEntity findMovementComemntByPage(Integer pageNum, Integer pageSize, String publishId) {
        //1.远程调用commentService查询评论的分页数据
        PageBeanVo pageBeanVo = commentService.findCommentByPage(new ObjectId(publishId), pageNum, pageSize);

        //2.获取分页中items(commentList),获取评论的发布人id,查询userInfo
        List<CommentVo> voList = new ArrayList<>();

        List<Comment> items = (List<Comment>) pageBeanVo.getItems();
        if (CollUtil.isNotEmpty(items)) {
            for (Comment comment : items) {
                //获取评论的发布人id,查询userInfo
                UserInfo userInfo = userInfoService.findByUserId(comment.getUserId());

                //3.封装userInfo和commnet为CommentVo对象,将每个vo放入voList中
                CommentVo vo = new CommentVo();
                vo.setId(comment.getId().toString());
                vo.setAvatar(userInfo.getAvatar());
                vo.setNickname(userInfo.getNickname());
                vo.setContent(comment.getContent());
                vo.setCreateDate(DateUtil.formatDateTime(new Date(comment.getCreated())));

                voList.add(vo);
            }
        }

        //4.将voList设置给分页对象,且返回
        pageBeanVo.setItems(voList);
        return ResponseEntity.ok(pageBeanVo);
    }
}
